package cfs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import cfs.databean.Customer;
import cfs.formbean.CreateCustomerFormBean;
import cfs.model.CustomerDAO;
import cfs.model.Model;

public class CreateCustomerAction extends Action {
	private FormBeanFactory<CreateCustomerFormBean> formBeanFactory = FormBeanFactory
			.getInstance(CreateCustomerFormBean.class);

        private EmailService emailService;
	private CustomerDAO customerDAO;

	public CreateCustomerAction(Model model) {
		customerDAO = model.getCustomerDao();
                emailService = new EmailService(model); 
	}

	public String getName() {
		return "createCustomer.do";
	}

	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		try {
			CreateCustomerFormBean form = formBeanFactory.create(request);
			request.setAttribute("form", form);

			if (!form.isPresent()) {
				return "CreateCustomer.jsp";
			}

			errors.addAll(form.getValidationErrors());
			if (errors.size() != 0) {
				return "CreateCustomer.jsp";
			}

			Customer user = customerDAO.lookup(form.getUsername());

			if (user != null) {
				errors.add("This user name already exist");
				return "CreateCustomer.jsp";
			}

			Customer customer = new Customer();
			customer.setAddr_line1(form.getAddr_line1());
			customer.setAddr_line2(form.getAddr_line2());
			customer.setCity(form.getCity());
			customer.setFirstname(form.getFirstname());
			customer.setLastname(form.getLastname());
			customer.setPassword(form.getPassword());
			customer.setState(form.getState());
			customer.setUsername(form.getUsername());
			customer.setZip(form.getZip());
			customer.setEmail(form.getEmail());
			customer.setCash(0);
			customer.setAvailablebalance(0);

			customerDAO.create(customer);

			// Attach (this copy of) the user bean to the session
			HttpSession session = request.getSession(false);
			//session.setAttribute("customer", customer);

                        //Send Email
                        emailService.activeEmailService(2, customer);
                        
			// After successful send to...
			String redirectTo = (String) session.getAttribute("redirectTo");
			if (redirectTo != null)
				return "CreateCustomer.jsp";

			// If redirectTo is null, redirect to the "manage" action
			// String webapp = request.getContextPath();
			else

				request.setAttribute("successStatement", form.getUsername() + " has been created");
				return "Success.jsp";
		} catch (DAOException e) {
			errors.add(e.getMessage());
			return "CreateCustomer.jsp";
		} catch (FormBeanException e) {
			errors.add(e.getMessage());
			return "CreateCustomer.jsp";
		}
	}
}
